package com.quxiu.system.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.quxiu.system.constant.BpmNodeType;
import com.quxiu.system.domain.BpmProcNode;
import com.quxiu.system.service.IdService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流节点生成
 */
public class BpmNodeGenerate {
    // 流程id
    private Long procId;
    // 版本id
    private Long versionId;
    // 节点配置
    private JSONObject nodeConfig;
    // 雪花id生成器
    private IdService idService;
    // 节点列表
    private List<BpmProcNode> nodes = new ArrayList<>();
    // 路由对应的跳转节点
    private Map<Long, Long> skipMap = new HashMap<>();

    public BpmNodeGenerate(IdService idService, Long procId, Long versionId, JSONObject nodeConfig) {
        this.procId = procId;
        this.versionId = versionId;
        this.nodeConfig = nodeConfig;
        this.idService = idService;
    }

    /**
     * 生成节点
     *
     * @return 节点列表
     */
    public List<BpmProcNode> generate() {
        this.generateNodes(nodeConfig, null, null);
        return nodes;
    }

    private Long generateNodes(JSONObject nodeConfig, Long parentId, Long routerId) {
        if (nodeConfig != null && !nodeConfig.isEmpty()) {
            BpmProcNode node = new BpmProcNode();
            node.setNodeId(idService.nextId());
            node.setProcId(procId);
            node.setVersionId(versionId);
            node.setParentId(parentId == null ? 0L : parentId);
            node.setRouterId(routerId);
            node.setNodeName(nodeConfig.getString("nodeName"));
            node.setNodeType(nodeConfig.getString("nodeType"));
            JSONObject config = nodeConfig.getJSONObject("config");
            if (config != null) {
                node.setConfig(config.toJSONString());
            }
            nodes.add(node);

            // 递归创建子节点
            JSONObject childNode = nodeConfig.getJSONObject("childNode");
            Long skipNodeId = null;
            if (childNode != null && !childNode.isEmpty()) {
                skipNodeId = generateNodes(childNode, node.getNodeId(), routerId);
            } else {
                if (routerId != null) {
                    node.setSkipNodeId(skipMap.get(routerId));
                }
            }

            // 如果是路由节点，则递归创建分支子节点
            if (BpmNodeType.ROUTER.equals(node.getNodeType())) {
                // 记录路由节点的跳转节点
                if (skipNodeId != null) {
                    skipMap.put(node.getNodeId(), skipNodeId);
                } else {
                    assert skipMap != null;
                    skipNodeId = skipMap.get(routerId);
                    skipMap.put(node.getNodeId(), skipNodeId);
                }


                JSONArray conditionNodes = nodeConfig.getJSONArray("conditionNodes");
                if (conditionNodes != null) {
                    for (int i = 0; i < conditionNodes.size(); i++) {
                        JSONObject conditionNode = conditionNodes.getJSONObject(i);
                        generateNodes(conditionNode, node.getNodeId(), node.getNodeId());
                    }
                }
            }

            return node.getNodeId();

        }
        return null;
    }
}
